// Thud - a dynamic object-oriented language
// Copyright (C) 2009,2010 David H. Hovemeyer <david.hovemeyer@gmail.com>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

/*
 * StringInputStream.cpp
 *
 *  Created on: Dec 14, 2009
 *      Author: dhovemey
 */

#include "util/EOFException.h"
#include "util/StringInputStream.h"

StringInputStream::StringInputStream(const std::string &input)
	: m_iter(input.begin())
	, m_end(input.end())
{

}

StringInputStream::~StringInputStream()
{

}

int StringInputStream::read()
{
	if (m_iter == m_end) {
		return -1;
	}
	int c = *m_iter;
	m_iter++;
	return c;
}

size_t StringInputStream::read(void *buf_, size_t n)
{
	char *buf = (char *) buf_;

	size_t numRead = 0;
	while (numRead < n) {
		if (m_iter == m_end) {
			break;
		}
		buf[numRead] = *m_iter;
		m_iter++;
	}

	if (numRead == 0) {
		throw EOFException(numRead);
	}

	return numRead;
}

bool StringInputStream::atEOF()
{
	return m_iter == m_end;
}
